/**
 * 
 */

$(function() {
	initFilter();

	initialCategory();

	$("#s").keydown(function(e) {
		var curKey = e.which;
		if (curKey == 13) {
			search();
		}
	});
});

function checkCategory(cid) {
	$("#s").attr('disabled', 'true');
	restoreSearch();
	$('#f-cate').attr('value', cid);
	refreshCategory();
}

function checkPerPage(value) {
	if (value != $('#f-each').val()) {
		$("#s").attr('disabled', 'true');
		$('#f-each').attr('value', value);
		$('#f-pres').val(1);
		refreshItems();
	}
}

function checkPerType(value) {
	if (value != $('#f-perType').val()) {
		restoreSearch();
		$("#s").attr('disabled', 'true');
		$('#f-type').attr('value', value);
		$('#f-pres').val(1);
		refreshItems();
	}
}

function naviTo(page) {
	$('#f-pres').attr('value', page);
	refreshItems();
}

function refreshItems() {
	$("#s").attr('disabled', 'true');
	$('#items-content').html('Loading products...');
	$('#breadcrumb').html('Loading...');
	$('.pagenavi').each(function() {
		$(this).html('&nbsp;');
	});
	var pres = $('#f-pres').val();
	var cate = $('#f-cate').val();
	var type = $('#f-type').val();
	var each = $('#f-each').val();
	var term = $('#f-term').val();
	var url = $('#item-url').val();
	$.post(url, {
		'pres' : pres,
		'cate' : cate,
		'type' : type,
		'each' : each,
		'term' : term,
	}, function(data) {
		var obj = json_parse(data);
		$('#items-content').html(obj.itemsHtml);
		$('.pagenavi').each(function() {
			$(this).html(obj.naviHtml);
		});
		$('#download_type').html(obj.downloadLink);
		$('#breadcrumb').html(obj.breadcrumb);
		$(".inline").fancybox({
			'titlePosition' : 'inside',
			'transitionIn' : 'none',
			'transitionOut' : 'none'
		});
		$(function() {
			$('.colleft').equalHeights();
		});
		$('#s').removeAttr('disabled');
	});
}
/**
 * refresh category depends on value of '#f-cate'
 * 
 * @returns null
 */
function refreshCategory() {
	$("#s").attr('disabled', 'true');
	$('#items-content').html('Loading category...');
	var cate = $('#f-cate').val();
	var url = $('#cate-url').val();
	$.post(url, {
		'cate' : cate
	}, function(data) {
		var obj = json_parse(data);
		$('#category-content').html(obj.categoryHtml);
		refreshItems();
	});
}

function initialCategory() {
	$("#s").attr('disabled', 'true');
	var cate = $('#f-cate').val();
	var url = $('#cate-url').val();
	$.post(url, {
		'cate' : cate
	}, function(data) {
		var obj = json_parse(data);
		$('#category-content').html(obj.categoryHtml);
	});
	$('#s').removeAttr('disabled');
}

function search() {
	var term = $('#s').val();
	if (term != 'Search...') {
		$('#f-term').val($('#s').val());
		$('#f-cate').val(0);
		$('#f-pres').val(1);
		refreshCategory();
	} else {
		alert('Please fill a term to search!');
	}
}

function downloadGroup(id) {
	var type = '';
	$('#download_type :checkbox').each(function() {
		if (this.checked) {
			type += $(this).attr('tid') + '-';
		}
	});
	type = type.substr(0, (type.length - 1));
	if (type.length == 0) {
		alert('Please choose one download type at least.');
		return;
	}

	var product = '';
	if (id == 0) {
		$('#items-content :checkbox').each(function() {
			if (this.checked) {
				product += $(this).attr('pid') + '-';
			}
		});
		product = product.substr(0, (product.length - 1));
		if (product.length == 0) {
			alert('Please choose one product at least.');
			return;
		}
	} else {
		product = id;
	}

	var url = $('#down-url').val();
	url = url + '?product=' + product + '&type=' + type;
	window.open(url, 'download', 'width=400,height=200')
}

function productDownload(id) {
	if (id == 0) {
		$('#download-btn').html('<a onclick="downloadGroup(0)">Download</a>');
	} else {
		$('#download-btn').html(
				'<a onclick="downloadGroup(' + id + ')">Download</a>');
	}
}

function initFilter() {
	$('#per_type option').each(function() {
		if ($(this).html() == 'Images-Front view') {
			$(this).attr('selected', 'selected');
			$('#f-type').val($(this).val());
		}
	});
	$('#f-pres').val(1);
	$('#f-term').attr('value','');
	$('#f-each').val(20);
	$('#f-cate').val(0);
}

function restoreSearch(){
	$('#s').val('Search...');
	$('#f-term').attr('value','');
}

